/**
 * VideoSwiper类 - 封装视频轮播相关逻辑
 */
export class VideoSwiper {
  currentIndex = 0;
  preIndex = 0;
  nextIndex = 0;
  current = 0;
  circular = false;
  curQueue = [];
  
  constructor(options = {}) {
    // 数据源
    this.list = options.list || [];
    // 初始seq
    this.seq = options.seq || 1;
    // 回调函数
    this.callbacks = {
      onQueueChange: options.onQueueChange || (() => {}),
      onCurrentChange: options.onCurrentChange || (() => {}),
      onCircularChange: options.onCircularChange || (() => {})
    };
  }
  
  // 初始化
  init() {
    // 1. 初始化数据
    this.currentIndex = this.seq - 1;
    this._changeCurrentIndex(this.currentIndex);
    
    // 2. 创建合适大小的队列
    this.curQueue = new Array(Math.min(3, this.list.length)).fill(null);
    
    // 3. 确定当前swiper位置策略
    const position = this._determineSwiperPosition(this.currentIndex);
    this.current = position;
    
    // 4. 填充队列数据
    this._fillQueue(position);
    
    // 5. 设置circular属性
    this.circular = this.canCircular();
    
    // 6. 通知状态变化
    this._notifyChanges();
  }
  
  // 处理滑动事件
  handleChange(detail) {
    const { current } = detail;
    const lastIndex = this.current;
    const diff = current - lastIndex;
    
    let direction;
    if (diff === 1 || diff === -2) {
      direction = 'down';
    } else if (diff === -1 || diff === 2) {
      direction = 'up';
    } else {
      return;
    }

    if (lastIndex === current) {
      return;
    }

    let targetIndex = this.currentIndex;

    if (direction === 'down') {
      targetIndex = this.nextIndex;
      // 阻止超出数据范围的滑动
      if (targetIndex >= this.list.length) {
        return;
      }
    } else {
      targetIndex = this.preIndex;
      // 阻止超出数据范围的滑动
      if (targetIndex < 0) {
        return;
      }
    }
    this._changeCurrent(current, targetIndex, direction);
  }
  
  // 更新数据源
  updateList(list) {
    this.list = list || [];
    this.init();
  }
  
  // 跳转到指定序号
  goToSeq(seq) {
    this.seq = seq;
    this.init();
  }
  
  // ========= 私有方法 =========
  
  // 更新索引
  _changeCurrentIndex(targetIndex) {
    this.currentIndex = targetIndex;
    this.preIndex = targetIndex - 1;
    this.nextIndex = targetIndex + 1;
  }
  
  // 根据索引确定最合适的swiper位置
  _determineSwiperPosition(index) {
    if (index === 0) {
      // 第一项放在位置0
      return 0;
    }
    if (index === this.list.length - 1) {
      // 最后一项放在位置2
      return 2;
    } 
    if (index % 3 === 2) {
      // 索引为2,5,8...(对应seq=3,6,9...)的项放在中间位置
      return 1;
    } 
    // 其他情况根据索引计算
    return index % 3;
  }
  
  // 填充队列数据
  _fillQueue(position) {
    // 清空队列
    this.curQueue = new Array(Math.min(3, this.list.length)).fill(null);
    
    // 当前项放入对应位置
    this.curQueue[position] = this.list[this.currentIndex];
    
    if (position === 0) {
      // 如果在位置0，填充后面的项
      if (this._checkNextIndexValid(this.nextIndex)) {
        this.curQueue[1] = this.list[this.nextIndex];
      }
      if (this._checkNextIndexValid(this.nextIndex + 1)) {
        this.curQueue[2] = this.list[this.nextIndex + 1];
      }
    } else if (position === 2) {
      // 如果在位置2，填充前面的项
      if (this._checkPreIndexValid(this.preIndex)) {
        this.curQueue[1] = this.list[this.preIndex];
      }
      if (this._checkPreIndexValid(this.preIndex - 1)) {
        this.curQueue[0] = this.list[this.preIndex - 1];
      }
    } else {
      // 如果在中间位置，填充前后项
      if (this._checkPreIndexValid(this.preIndex)) {
        this.curQueue[0] = this.list[this.preIndex];
      }
      if (this._checkNextIndexValid(this.nextIndex)) {
        this.curQueue[2] = this.list[this.nextIndex];
      }
    }
  }
  
  // 是否开启circular属性
  canCircular() {
    // 只有当不在首尾项时才开启circular
    return this.currentIndex > 0 && this.currentIndex < this.list.length - 1
  }
  
  // 处理current变化
  _changeCurrent(current, targetIndex, direction) {
    if (targetIndex < 0 || targetIndex >= this.list.length) {
      return;
    }

    // 更新数据索引
    this._changeCurrentIndex(targetIndex);
    
    // 根据目标索引确定显示策略
    if (targetIndex === 0 || targetIndex === this.list.length - 1) {
      // 到达首尾项，完全重置队列和位置
      const position = (targetIndex === 0) ? 0 : 2;
      this.current = position;
      this._fillQueue(position);
    }  else {
      // 中间项，正常处理
      this.current = current;
      this._preload(1, direction);
    }
    
    // 更新circular属性
    this.circular = this.canCircular();
    
    // 通知状态变化
    this._notifyChanges();
  }
  
  // 预加载数据
  _preload(count, direction) {
    if (direction === 'down') {
      this._preloadDown(count);
    } else if (direction === 'up') {
      this._preloadUp(count);
    }
  }
  
  // 向下预加载指定数量的项
  _preloadDown(count) {
    
    for (let i = 1; i <= count; i++) {
      // 计算数据索引
      const nextDataIndex = this.currentIndex + i;
      
      // 检查数据边界
      if (!this._checkNextIndexValid(nextDataIndex)) {
        break;
      }
      
      // 计算在swiper中的位置索引
      const nextSwiperIndex = this.current + i;
      let normalizedSwiperIndex = this._formatSwiperIndex(nextSwiperIndex);
      // 特殊情况：当滑动到最后一项且当前位置是2
      if (nextDataIndex === this.list.length - 1 && this.current === 2) {
        normalizedSwiperIndex = 3;
      }
      
      // 预加载数据
      this.curQueue[normalizedSwiperIndex] = this.list[nextDataIndex];
    }
  }
  
  // 向上预加载指定数量的项
  _preloadUp(count) {
    
    for (let i = 1; i <= count; i++) {
      // 计算数据索引
      const prevDataIndex = this.currentIndex - i;
      
      // 检查数据边界
      if (!this._checkPreIndexValid(prevDataIndex)) {
        break;
      }
      
      // 计算在swiper中的位置索引
      const prevSwiperIndex = this.current - i;
      const normalizedSwiperIndex = this._formatSwiperIndex(prevSwiperIndex);
      
      // 预加载数据
      this.curQueue[normalizedSwiperIndex] = this.list[prevDataIndex];
    }
  }
  
  // 格式化swiper索引
  _formatSwiperIndex(index) {
    if (index < 0) {
      const x = index % 3;

      if (x === 0) {
        return 0;
      }

      return 3 + x;
    }

    return index % 3;
  }
  
  // 检查前一个索引是否有效
  _checkPreIndexValid(preIndex) {
    return preIndex >= 0;
  }
  
  // 检查下一个索引是否有效
  _checkNextIndexValid(nextIndex) {
    return nextIndex < this.list.length;
  }
  
  // 通知状态变化
  _notifyChanges() {
    // 通知当前索引变化
    this.callbacks.onCurrentChange(this.current);
    
    // 通知数据变化
    this.callbacks.onQueueChange(this.curQueue);
    // 通知循环状态变化
    this.callbacks.onCircularChange(this.circular);
  }
} 